<?php

namespace App\Http\Controllers\Frontend\V10\Message;

use App\Http\Controllers\Controller;
use App\Http\Requests\Frontend\V10\SysMessageIndexRequest;
use App\Models\HeaderMessage;
use App\Models\HeaderMessageReadeds;
use App\Models\MessageSended;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;

class HeaderController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth:api')->except('show');
    }

    /**
     * Description:获取当前有效的页面消息
     * Author: Sam
     * DateTime: 2019/1/12 14:43
     * @return mixed
     */
    public function show()
    {
        $now = date('Y-m-d H:i:s');
        $msg = HeaderMessage::where('status', 1)
            ->where('valid_time', '>=', $now)
            ->get();
        return success($msg);
    }

    /**
     * Description:公告列表
     * Author: wjl
     * DateTime: 2019-04-11 14:33
     * @param SysMessageIndexRequest $request
     * @return mixed
     */
    public function index(SysMessageIndexRequest $request)
    {
        $page_size = $request->get('page_size', null);
        if ($page_size) {
            $list = HeaderMessage::select('id', 'title', 'desc', 'content', 'is_h5', 'created_at', 'url')
                ->orderByDesc('id')
                ->paginate($page_size);
        } else {
            $list = HeaderMessage::select('id', 'title', 'desc', 'content', 'is_h5', 'created_at', 'url')
                ->orderByDesc('id')
                ->get();
        }

        $user_id = Auth::user()->id;
        if ($list) {
            $list = $list->toArray();
            if ($page_size) {
                $data['list'] = $this->_dataFormat($list['data'], $user_id);
                $data['total'] = $list['total'];
            } else {
                $data = $this->_dataFormat($list, $user_id);
            }
        }
        return success($data);
    }

    //格式化数据
    protected function _dataFormat($data, $user_id)
    {
        $arr = array();
        foreach ($data as $v) {
            $is_read = HeaderMessageReadeds::where('user_id', $user_id)->where('msg_id', $v['id'])->first();
            $v['is_read'] = $is_read ? 1 : 2;
            $arr[] = $v;
        }
        return $arr;
    }

    /**
     * Description:已读公告
     * Author: wjl
     * DateTime: 2019-04-11 18:06
     * @param Request $request
     */
    public function msgRead(Request $request)
    {

        $user_id = Auth::user()->id;
        $msg_id = $request->get('msg_id');

        if (!$user_id || !$msg_id) {
            return failed('参数错误');
        }

        HeaderMessageReadeds::create(['user_id' => $user_id, 'msg_id' => $msg_id]);

        return success('');
    }

    /**
     * Description: 所有消息已读
     * Author: wjl
     * DateTime: 2019-04-12 13:45
     * @param Request $request
     */
    public function msgAllRead(Request $request)
    {
        $user_id = Auth::user()->id;
        $msg_type = $request->get('msg_type');

        if (!$user_id) {
            return failed('参数错误');
        }

        if (!in_array($msg_type, [1, 2])) {
            return failed('类型错误');
        }

        if ($msg_type == 1) {
            MessageSended::where('user_id', $user_id)->update(['status' => 1]);
        } else {
            $readeds = HeaderMessageReadeds::select('msg_id')->where('user_id', $user_id)->get();
            if ($readeds) {
                $readeds = array_column($readeds->toArray(), 'msg_id');
                $ids = HeaderMessage::select('id')->whereNotIn('id', $readeds)->get();
            } else {
                $ids = HeaderMessage::select('id')->get();
            }

            if ($ids) {
                $ids = $ids->toArray();
                $arr = array();
                foreach ($ids as $v) {
                    $arr[] = ['msg_id' => $v['id'], 'user_id' => $user_id];
                }
                HeaderMessageReadeds::insert($arr);
            }
        }
        return success('');
    }
}
